var app = getApp();
Page({
    data:{
        siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
        mess : '',
        diss : false,
        dismoney : '',
        payfee : 0,
        yh : 0,
        allcard : [],
        isshowcard : false,
        carddis : 0,
        paytype : 0,
        isprize : 0,
        prize : {},
        prizeoid : '',
        Time : app.globalData.Time,
        spayf : '',
        spaya : '',
        tiptype : 1,
        isjfdk : 0,
        isshowjf : false,
        columns : [],
        isshowsuccess : false,
        paysucdata : [],
        ispaying : false,
        timedesc : 5,
        payres : {},
    },
    onLoad:function(options){
        var self = this;
        this.data.options = options;
        app.com.auth(function(user){
            var postdata = {
                op : 'info',
                sid : options.scene ? options.scene : ( options.shopid ? options.shopid : 0 ),
                isposter : options.scene || options.isposter*1 == 1 ? 1 : 0,
            };
            app.com.http('shoppay','POST',postdata,0,false,'','',function(res){
                if(!res.data.errno  && res.data){
                    self.setData({
                        sharedata : res.data.data.sharedata,
                        params : res.data.data.params,
                        shop : res.data.data.shop,
                        userinfo : res.data.data.userinfo,
                        allcard : res.data.data.allcard,
                        columns : res.data.data.columns,
                        isjfdk : 0,
                        jifendis : 0,
                        inited : true,
                    },function(){
                        if(self.data.money > 0){
                            self.countres();
                        }
                    });

                    if(res.data.data.params.spaytip == 1 && res.data.data.spayf){
                        var octoparse = require('../resource/wxParse/html2json2.js');
                        var transData = octoparse.parse(res.data.data.spayf);                   
                        self.setData({
                            spayf : transData,
                        })
                        var transData = octoparse.parse(res.data.data.spaya);                   
                        self.setData({
                            spaya : transData,
                        })
                    }

                }else{
                    app.com.alert(res.data.message,function(){
                        wx.navigateBack();
                    });
                }
            })
        });

        wx.setNavigationBarTitle({
            title:  '支付',
        });
    },
    onPullDownRefresh: function () {
        this.onLoad(this.data.options);
        wx.stopPullDownRefresh();
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: self.data.sharedata.title,
            path: self.data.sharedata.path,
            imageUrl : self.data.sharedata.img,
        }
    },
    onChangepaytype(e){
        this.setData({
            paytype : e.currentTarget.dataset.type,
        })
    },
    dontdeal(){},
    changeTakeType( e ){
        var type = e.currentTarget.dataset.type;
        if( type == this.data.taketype ) return false;
        this.setData({
            taketype : type
        });
        this.countData();
    },
    FormIdsubmit : function(e){
        if( app.globalData.formarr.length <= 20 ) app.globalData.formarr.push(e.detail.formId);
    },
    messchange( e ){
        this.setData({
            mess : e.detail.value,
        });
    },
    dissfn(){
        this.setData({
            diss : !this.data.diss
        })
    },
    money(e){
        this.setData({
            money : e.detail.value,
        });
        this.countres();
    },
    dismoney(e){
        this.setData({
            dismoney : e.detail.value,
        });
        this.countres();
    },
    countres(){
        
        var cut = this.data.shop.spcut > 0 && this.data.shop.spcut < 10 ? this.data.shop.spcut*1 : 10;
        var payfee = (this.data.money*1 - this.data.dismoney*1)*cut/10 + this.data.dismoney*1;
        var yh = this.data.money*1 - payfee;
        var give = ( payfee * this.data.params.spgjf/100).toFixed(2) * 1;

        var jifendis = 0;
        var isjfdk = 0;

        if( this.data.usecardid > 0 ){
            for (var i = 0; i < this.data.allcard.length; i++) {
                if( this.data.allcard[i].id == this.data.usecardid ){
                    if( payfee*1 >= this.data.allcard[i].use*1 ){
                        payfee -= this.data.allcard[i].fee*1;
                        yh += this.data.allcard[i].fee*1;
                        this.setData({
                            usecardid : this.data.allcard[i].id,
                            usecardname : this.data.allcard[i].name,
                            carddis : this.data.allcard[i].fee,
                        })
                    }else{
                        this.setData({
                            usecardid : 0,
                            carddis : 0,
                            usecardname : '',
                        })
                    }
                }
            }
        }

        // 积分抵扣
        if(this.data.isjfdk == 1 && payfee > 0){
            jifendis = payfee*1 >= this.data.params.maxjfdk*1 ? this.data.params.maxjfdk*1 : payfee;
            var candis = (payfee * this.data.params.spmaxjfper/100).toFixed(2);
            jifendis = candis*1 >= jifendis ? jifendis : candis*1;
            payfee -= jifendis*1;
            yh += jifendis*1;
            isjfdk = 1;
        }else{
            isjfdk = 0;
        }

        payfee = payfee <= 0 ? 0 : payfee;

        this.setData({
            givejf : give >= 0.01 ? give : 0,
            payfee : payfee.toFixed(2) * 1,
            yh : yh.toFixed(2) * 1,
            jifendis : jifendis,
            isjfdk : isjfdk,
        });
    },
    mess(e){
        this.setData({
            mess : e.detail.value,
        });
    },
    paypay(){
        if(this.data.spayf){
            this.setData({
                isshowtip : true,
                tiptype : 1,
            })
        }else{
            this.confirm();
            this.setData({
                isshowtip : false,
            })
        }
    },
    confirmfn(){
        if(this.data.tiptype == 1){
            this.confirm();
            this.setData({
                isshowtip : false,
            })
        }else{
            this.setData({
                isshowtip : false,
            })
        }
    },
    confirm(e){
        var self = this;
        
        if( !self.data.name && self.data.taketype == 1 && self.data.params.coname == 0 ) {
            app.com.toast('请填写姓名');
            return false;
        }
        if( !app.com.verify('mobile', self.data.tel ) && self.data.taketype == 1 && self.data.params.cotel == 0 ) {
            app.com.toast('请填写正确的电话号码');
            return false;
        }
        var postdata = {
            op:'pay',
            sid : self.data.shop.id,
            mess : self.data.mess,
            money : self.data.money,
            dismoney : self.data.dismoney,
            usecardid : self.data.usecardid ? self.data.usecardid : 0,
            paytype : self.data.paytype,
            jifendis : self.data.jifendis,
        };
        if( self.data.payed ) return false;
        self.data.payed = true;

        app.com.http('shoppay','POST',postdata,0,true,'','',function(res){
            self.data.payed = false;
            if (!res.data.errno && res.data) {

                if(postdata.paytype*1 == 1){
                    self.setData({
                        'userinfo.money' : res.data.data.lastumoney
                    })
                }

                if( res.data.data.payed == 1 ){
                    self.data.payres = res;
                    self.paysuccessmess(res);

                    /*app.com.alert('支付成功',function(){
                        self.successMess(res);
                    });*/
                    return false;
                }

                wx.requestPayment({
					'appId' : res.data.data.appId,
                    'timeStamp': res.data.data.timeStamp,
                    'nonceStr': res.data.data.nonceStr,
                    'package': res.data.data.package,
                    'signType': 'MD5',
                    'paySign': res.data.data.paySign,
                    'success': function (payres) {
                        if( payres.errMsg == 'requestPayment:ok' ){
                            self.data.payres = res;
                            self.paysuccessmess(res);
                        }
                    },
                    fail: function (payres) {

                        if(postdata.paytype*1 == 1){
                            app.com.alert('取消订单，扣除的余额将在稍后返还到账户余额中');
                        }

                        self.onLoad(self.data.options);
                        self.setData({
                            usecardid : '',
                            usecardname : '',
                            isjfdk : 0,
                            jifendis : 0,
                            carddis : 0,
                        });
                    }
                });
                
            }else{
                app.com.alert(res.data.message);
            }
        });
        
    },
    paysuccessmess(res){
        var self = this;
        self.setData({
            isshowsuccess : true,
            ispaying : true,
            paysucdata : res.data.data.paysucdata,
            timedesc : self.data.params.spydtm*1,
        })
        self.timedescfn();
    },
    onclicksuccess(){
        this.successMess(this.data.payres);
    },
    successMess(res){
        var self = this;

        self.setData({
            usecardname : '',
            usecardid : '',
            isjfdk : 0,
            jifendis : 0,
            carddis : 0,
            isshowsuccess : false,
        })
        if(res.data.data.prize && res.data.data.prize.isprize == 1){
            self.setData({
                prize : res.data.data.prize,
                isprize : 1,
                prizeoid : res.data.data.oid,
            },function(){
                self.selectComponent("#payorderprize").getLottery(res.data.data.prize,res.data.data.oid);
            })
        }else{
            if(res.data.data.showchb == 1){
                self.selectComponent("#paychb").showhb(res.data.data.showchboid,res.data.data.showchbfee);
            }else{
                self.paysuc(res.data.data);
            }
        }
    },
    paysuc(){
        var self = this;
        self.setData({
            mess : '',
            money : '',
        });
        if(self.data.spaya){
            self.setData({
                isshowtip : true,
                tiptype : 2,
            })
        }
        if(self.data.params.spwxurl){
            setTimeout(function(){
                var ea = {
                    currentTarget : {
                        dataset : {
                            url : self.data.params.spwxurl
                        }
                    }
                }
                app.com.navigateto(ea);
            },1000)
        }
    },
    paylott(e){
        this.paysuc();
    },
    paychbed(){
        this.paysuc();
    },
    showjfdis(){
        this.setData({
            isshowjf : !this.data.isshowjf,
        })
    },
    onChangejf(e){
        this.setData({
            isshowjf : false,
            isjfdk : e.detail.index,
        })
        this.countres();
    },
    navigateto : function( e ){
        app.com.navigateto( e,this.data.tid );
    },
    showcard(){
        this.setData({
            isshowcard : !this.data.isshowcard,
        });
    },
    usecard(e){
        var self = this;
        self.setData({
            usecardid : e.currentTarget.dataset.id,
        });
        self.countres();
        self.showcard();
    },
    showtip(){
        this.setData({
            isshowtip : !this.data.isshowtip
        })
    },
    timedescfn(){
        var self = this;
        if(self.data.timedesc > 0){
            setTimeout(function(){
                self.data.timedesc--
                self.setData({
                    timedesc : self.data.timedesc
                })
                self.timedescfn();
            },1000)
        }else{
            self.setData({
                ispaying : false,
            })
        }
    }

})